namespace net.podhola.gtd.controllers

import System
import System.Configuration
import System.Globalization
import System.Web
import System.Web.UI
import System.Web.UI.WebControls
import Castle.MonoRail.Framework
import net.podhola.gtd.finances

/* Scaffolding( EnteredBill ) */
[Layout("default")]
class EnteredBillsController(GTDParentController):

	override def Title():		
		return "Bills"

	def FindBill( id as int ) as EnteredBill:
		PropertyBag["Bill"] = EnteredBill.Find( id )
		return PropertyBag["Bill"]

	def Detail( id as int ):
		FindBill( id )

	def Edit( id as int ):
		FindBill( id )

	def New():
		pass

	def Pay( id as int ):
		FindBill( id )

	def Delete( id as int ):
		FindBill( id ).Delete()
		RedirectToAction("Index" )

	def Create( Subject, Amount, ExpectedPaymentDate, FromAccount ):
		bill = EnteredBill()
		accountid as int = Int32.Parse( Form[ "FromAccount" ] )
		bill.PlannedAccount = Account.Find( accountid )
		
		amount as double = Double.Parse( Form[ "Amount" ], NumberStyles.Currency )
		bill.PlannedAmount = amount
		bill.Subject = Subject
		bill.ExpectedPaymentDate  = DateTime.Parse( ExpectedPaymentDate)			
		
		bill.Create()

		PropertyBag["Bill"] = bill

	def Update( [DataBind("Bill")] bill as EnteredBill ):
		accountid as int = Int32.Parse( Form[ "Bill.PlannedAccount" ] )
		bill.PlannedAccount = Account.Find( accountid )
		amount as double = Double.Parse( Form[ "Bill.PlannedAmount" ], NumberStyles.Currency )
		bill.PlannedAmount = amount
		bill.Save()
		RedirectToAction("Detail", { "id":bill.Id } )

	def GetAmountDue() as double:
		amountDue as double = 0.0
		allBills = EnteredBill.FindAll()
		for bill in allBills:
			amountDue = amountDue + bill.PlannedAmount - bill.GetAlreadyPaied()
		return amountDue

	override def GetWelcomeNumber() as string:
		return GetAmountDue().ToString("c")
		
	override def GetWelcomeText() as string:
		return "due"
